<template><div><h2 id="laravel-pint" tabindex="-1"><a class="header-anchor" href="#laravel-pint"><span>Laravel Pint</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#running-pint">运行 Pint</a></li>
<li><a href="#configuring-pint">配置 Pint</a>
<ul>
<li><a href="#presets">Presets (预设)</a></li>
<li><a href="#rules">规则</a></li>
<li><a href="#excluding-files-or-folders">排除文件/文件夹</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/pint" target="_blank" rel="noopener noreferrer">Laravel Pint</a> 是一款面向极简主义者的 PHP 代码风格固定工具。Pint 是建立在 PHP-CS-Fixer 基础上，使保持代码风格的整洁和一致变得简单。</p>
<p>Pint 会随着所有新的 Laravel 应用程序自动安装，所以你可以立即开始使用它。默认情况下，Pint 不需要任何配置，将通过遵循 Laravel 的观点性编码风格来修复你的代码风格问题。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>Pint 已包含在 Laravel 框架的最近版本中，所以无需安装。然而，对于旧的应用程序，你可以通过 Composer 安装 Laravel Pint：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/pint <span class="token parameter variable">--dev</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="运行-pint" tabindex="-1"><a class="header-anchor" href="#运行-pint"><span>运行 Pint</span></a></h2>
<p>可以通过调用你项目中的 <code v-pre>vendor/bin</code> 目录下的 <code v-pre>pint</code> 二进制文件来指示 Pint 修复代码风格问题：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以在特定的文件或目录上运行 Pint：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint app/Models</span>
<span class="line"></span>
<span class="line">./vendor/bin/pint app/Models/User.php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Pint 将显示它所更新的所有文件的详细列表。 你可以在调用 Pint 时提供 <code v-pre>-v</code> 选项来查看更多关于 Pint 修改的细节。：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint <span class="token parameter variable">-v</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你只想 Pint 检查代码中风格是否有错误，而不实际更改文件，则可以使用 <code v-pre>--test</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint <span class="token parameter variable">--test</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你希望 Pint 根据 Git 仅修改未提交更改的文件，你可以使用 <code v-pre>--dirty</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint <span class="token parameter variable">--dirty</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="配置-pint" tabindex="-1"><a class="header-anchor" href="#配置-pint"><span>配置 Pint</span></a></h2>
<p>如前面所述，Pint 不需要任何配置。但是，如果你希望自定义预设、规则或检查的文件夹，可以在项目的根目录中创建一个 <code v-pre>pint.json</code> 文件：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">  <span class="token property">"preset"</span><span class="token operator">:</span> <span class="token string">"laravel"</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，如果你希望使用特定目录中的 <code v-pre>pint.json</code>，可以在调用 Pint 时提供 <code v-pre>--config</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">pint <span class="token parameter variable">--config</span> vendor/my-company/coding-style/pint.json</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="presets-预设" tabindex="-1"><a class="header-anchor" href="#presets-预设"><span>Presets(预设)</span></a></h3>
<p>Presets 定义了一组规则，可以用来修复代码风格问题。默认情况下，Pint 使用 laravel preset，通过遵循 <code v-pre>Laravel</code> 的固定编码风格来修复问题。但是，你可以通过向 Pint 提供 <code v-pre>--preset</code> 选项来指定一个不同的 preset 值：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">pint <span class="token parameter variable">--preset</span> psr12</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你愿意，还可以在项目的 <code v-pre>pint.json</code> 文件中设置 preset ：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">  <span class="token property">"preset"</span><span class="token operator">:</span> <span class="token string">"psr12"</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Pint 目前支持的 presets 有：<code v-pre>laravel</code>、<code v-pre>psr12</code> 和 <code v-pre>symfony</code>。</p>
<h3 id="规则" tabindex="-1"><a class="header-anchor" href="#规则"><span>规则</span></a></h3>
<p>规则是 Pint 用于修复代码风格问题的风格指南。如上所述，presets 是预定义的规则组，适用于大多数 PHP 项目，因此你通常不需要担心它们所包含的单个规则。</p>
<p>但是，如果你愿意，可以在 <code v-pre>pint.json</code> 文件中启用或禁用特定规则：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"preset"</span><span class="token operator">:</span> <span class="token string">"laravel"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"rules"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"simplified_null_return"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"braces"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"new_with_braces"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"anonymous_class"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token property">"named_class"</span><span class="token operator">:</span> <span class="token boolean">false</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Pint是基于 <a href="https://github.com/FriendsOfPHP/PHP-CS-Fixer" target="_blank" rel="noopener noreferrer">PHP-CS-Fixer</a> 构建的。因此，您可以使用它的任何规则来修复项目中的代码风格问题： <a href="https://mlocati.github.io/php-cs-fixer-configurator" target="_blank" rel="noopener noreferrer">PHP-CS-Fixer Configurator</a>.</p>
<h3 id="排除文件-文件夹" tabindex="-1"><a class="header-anchor" href="#排除文件-文件夹"><span>排除文件/文件夹</span></a></h3>
<p>默认情况下，Pint将检查项目中除 <code v-pre>vendor</code> 目录以外的所有 <code v-pre>.php</code> 文件。如果您希望排除更多文件夹，可以使用 <code v-pre>exclude</code> 配置选项:</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"exclude"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string">"my-specific/folder"</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您希望排除包含给定名称模式的所有文件，则可以使用 <code v-pre>notName</code> 配置选项:</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"notName"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string">"*-my-file.php"</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您想要通过提供文件的完整路径来排除文件，则可以使用 <code v-pre>notPath</code> 配置选项:</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"notPath"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string">"path/to/excluded-file.php"</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/pint/14912" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/pi...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/pint/14912" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/pi...</a></p>
</blockquote>
</div></template>


